package com.chunayev.numerology.gui.list;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Action;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import com.chunayev.numerology.domain.PositionSummary;
import com.chunayev.numerology.gui.action.SummaryTokenChangeNotifier;
import com.chunayev.numerology.gui.frame.SummaryFrame;
import com.sleepycat.je.DatabaseException;

/**
 * @author Sergey Chunayev
 */
public class EntityList extends JList implements SummaryTokenChangeNotifier,
        ListSelectionListener {
    
    private PositionSummary positionSummary;
    
    private String token;
    
    private Action listMenuAction;
    
    private final JPopupMenu popupMenu = new JPopupMenu();
    
    private SummaryFrame summaryFrame;
    
    /**
     * 
     */
    private static final long serialVersionUID = 1024469501105184411L;
    
    /**
     * 
     */
    public EntityList() {
        this((PositionSummary) null);
    }
    
    /**
     * @param listMenuAction
     */
    public EntityList(final Action listMenuAction) {
        this((PositionSummary) null);
        setListMenuAction(listMenuAction);
    }
    
    /**
     * @param positionSummary
     */
    public EntityList(final PositionSummary positionSummary) {
        super();
        this.positionSummary = positionSummary;
        addListSelectionListener(this);
        this.addMouseListener(new ListMenuInvoker());
    }
    
    /**
     * @return the listMenuAction
     */
    public Action getListMenuAction() {
        return this.listMenuAction;
    }
    
    /**
     * @return the positionSummary
     */
    public PositionSummary getPositionSummary() {
        return this.positionSummary;
    }
    
    /*
     * (non-Javadoc)
     * @see
     * com.chunayev.numerology.gui.action.SummaryTokenChangeNotifier#getSummary()
     */
    @Override
    public PositionSummary getSummary() throws DatabaseException {
        return this.positionSummary;
    }
    
    /**
     * @return the summaryFrame
     */
    public SummaryFrame getSummaryFrame() {
        return this.summaryFrame;
    }
    
    /*
     * (non-Javadoc)
     * @see com.chunayev.numerology.gui.action.SummaryTokenChangeNotifier#getToken()
     */
    @Override
    public String getToken() {
        return this.token;
    }
    
    /**
     * @param listMenuAction
     *            the listMenuAction to set
     */
    public void setListMenuAction(final Action listMenuAction) {
        this.listMenuAction = listMenuAction;
        popupMenu.add(this.listMenuAction);
    }
    
    /**
     * @param positionSummary
     *            the positionSummary to set
     */
    public void setPositionSummary(final PositionSummary positionSummary) {
        this.positionSummary = positionSummary;
    }
    
    /**
     * @param summaryFrame
     *            the summaryFrame to set
     */
    public void setSummaryFrame(final SummaryFrame summaryFrame) {
        this.summaryFrame = summaryFrame;
    }
    
    /*
     * (non-Javadoc)
     * @see com.chunayev.numerology.gui.action.SummaryTokenChangeNotifier#update()
     */
    @Override
    public void update() {
        summaryFrame.update(this);
    }
    
    /*
     * (non-Javadoc)
     * @see
     * javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event
     * .ListSelectionEvent)
     */
    @Override
    public void valueChanged(final ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
            JList list = (JList) e.getSource();
            if (list.getSelectedIndex() != -1) {
                this.token = (String) list.getSelectedValue();
            } else {
                this.token = null;
            }
        }
    }
    
    private class ListMenuInvoker extends MouseAdapter {
        
        /*
         * (non-Javadoc)
         * @see
         * java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
         */
        @Override
        public void mousePressed(final MouseEvent e) {
            super.mousePressed(e);
            showPopup(e);
        }
        
        /*
         * (non-Javadoc)
         * @see
         * java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
         */
        @Override
        public void mouseReleased(final MouseEvent e) {
            super.mouseReleased(e);
            showPopup(e);
        }
        
        private void showPopup(final MouseEvent e) {
            if (e.isPopupTrigger() && popupMenu != null) {
                int index = locationToIndex(e.getPoint());
                if (index != -1) {
                    listMenuAction.setEnabled(true);
                    setSelectedIndex(index);
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                } else {
                    listMenuAction.setEnabled(false);
                }
            }
        }
    }
    
}
